package xyz.xiaomeiqiu.study.util;

import xyz.xiaomeiqiu.study.domain.Student;

import java.sql.*;

public class JdbcConn {

    private static final String ADDR = "jdbc:mysql://192.168.255.253:3306/geekbang?useUnicode=true&characterEncoding=utf-8&useSSL=false";
    private static final String USER = "root";
    private static final String PWD = "mysql123";

    private static final String INSERT = "insert into Student(name) values(?)";
    private static final String QUERY_ONE = "select id, name from Student where id = (?)";
    private static final String UPDATE_ONE = "update Student set name = (?) where id = (?)";
    private static final String DELETE = "delete from Student where id = (?)";

    public static void test() throws SQLException {
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        //注册数据库驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //取得数据库连接
            conn = DriverManager.getConnection(ADDR, USER, PWD);

            // 增
            preparedStatement = conn.prepareStatement(INSERT);
            Student student = new Student();
            student.setName("王麻子");
            int insert = insert(student, preparedStatement);
            System.out.println("新增了" + insert + "条数据");
            Student student1 = new Student();
            student1.setName("刘强");
            int insert1 = insert(student, preparedStatement);
            System.out.println("新增了" + insert1 + "条数据");

            // 删
            preparedStatement = conn.prepareStatement(DELETE);
            int delete = deleteOne(1, preparedStatement);
            System.out.println("删除了" + delete + "条数据");

            // 改
            preparedStatement = conn.prepareStatement(UPDATE_ONE);
            Student user = new Student();
            user.setName("测试");
            user.setId(2);
            int i = updateOne(user, preparedStatement);
            System.out.println("修改了" + i + "条数据");

            // 查
            preparedStatement = conn.prepareStatement(QUERY_ONE);
            rs = queryOne(2, preparedStatement);
            while (rs.next()) {
                System.out.println(rs.getString("name"));
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            closeResource(conn, preparedStatement, null);
        }
    }

    public static void closeResource(Connection connection, PreparedStatement preparedStatement, ResultSet rs) throws SQLException {
        if (rs != null) {
            rs.close();
        }
        if (preparedStatement != null) {
            preparedStatement.close();
        }
        if (connection != null) {
            connection.close();
        }
    }

    private static int insert(Student student, PreparedStatement preparedStatement) throws SQLException {
        preparedStatement.setString(1, student.getName());
        return preparedStatement.executeUpdate();
    }

    private static int deleteOne(Integer id, PreparedStatement preparedStatement) throws SQLException {
        preparedStatement.setLong(1, id);
        return preparedStatement.executeUpdate();
    }

    private static int updateOne(Student student, PreparedStatement preparedStatement) throws SQLException {
        preparedStatement.setString(1, student.getName());
        preparedStatement.setLong(2, student.getId());
        return preparedStatement.executeUpdate();
    }

    private static ResultSet queryOne(Integer id,  PreparedStatement preparedStatement) throws SQLException {
        preparedStatement.setLong(1, id);
        return preparedStatement.executeQuery();
    }
}
